
<!--setOrgInfo():返回数据id-->
<!--setOrgname():返回审核人-->
<!--orgTree:true   选择机构-->
<!--codeAffordIf:true   筛选管理机构-->
<!--currentOrgId:orgId   用于修改页面默认展示当前人员的机构ID-->
<!--currentIndex:index   用于返回索引-->
<!--currentOrgName:orgName   用于修改页面默认展示当前人员的机构Name-->
<!--setCurrentName:{value:123,label:'王德发'}   组件默认显示-->
<!--multiple:true   可多选下级审核人-->
<!--rootOrgId 从该机构id -->

<template>
    <div class="Cascader">
        <Select v-model="selectiondata" :disabled='selectionarr.length==0' placeholder='请点击右侧添加按钮添加人员' multiple style="width:50%;float: left;" label-in-value @on-change='selectchange' v-if='multiple'>
            <Option v-for="item in selectionarr" :value="item.value" :key="item.value">{{ item.label }}</Option>
        </Select>
        <Button   @click="setselectiondata" v-if='!setselectionshow && multiple'><Icon type="md-add" /></Button>
        <Cascader v-if='setselectionshow && multiple' :style="multiple?'width: 50%;float: left;':''" :change-on-select="orgTree" :data="paramsdatacopy" v-model="paramscopy" :render-format="Cascaderformat" :load-data="loadData" @on-visible-change="handleChangeOnSelect"></Cascader>
        <Cascader v-if='!multiple' :style="multiple?'width: 50%;float: left;':''" :change-on-select="changeOnSelect" :data="paramsdatacopy" v-model="paramscopy" :render-format="Cascaderformat" :load-data="loadData" @on-visible-change="handleChangeOnSelect"></Cascader>
    </div>
</template>

<script>
    import { sessionStorage } from '@/common/cacheUtils.js';
    import {Api} from '@/api/index.js'

    export default {
        props: {
            orgTree:{
                type: Boolean,
                default: false
            },
            codeAffordIf:{
                type: String,
                default: '0'
            },
            multiple:{
                type: Boolean,
                default: false
            },
            setselectionarr:{
                type: Array,
                default: ()=>[]
            },
            setCurrentName:{
            },
            currentOrgId : {
            },
            currentOrgName : {
            },
            currentIndex:{},
            rootOrgId: null
        },
        data() {
            return {
                setselectionshow:false,
                selectiondata:[],
                selectiondatacopy:[],
                selectionarr:this.setselectionarr.length!=0?this.setselectionarr:[],
                paramscopy:this.setCurrentName?[this.setCurrentName.value]:[],
                paramsdatacopy:this.setCurrentName?[this.setCurrentName]:[],
                arrcopy:[],
                changeOnSelect : false,
            }
        },
        methods: {
            selectchange(value){
                this.$emit('setOrgInfo',value)
            },
            setselectiondata(){
                this.setselectionshow = true
            },
            setUsers(){
                this.selectionarr = [];
                this.selectiondata = [];
                this.arrcopy = [];
                this.setselectionarr.map(i=>{
                    this.selectionarr.push(i);
                    this.selectiondata.push(i.value);
                    this.arrcopy.push(i.label);
                })
            },
            setCurrentUser(){
                //this.resetCurrentOrg();
                this.paramsdatacopy=[{
                    value : this.setCurrentName.value,
                    label : this.setCurrentName.label,
                }];
                this.paramscopy = [this.setCurrentName.value];
            },
            setCurrentOrg(){
                //this.resetCurrentOrg();
                this.paramsdatacopy=[{
                    value :  this.currentOrgId,
                    label :  this.currentOrgName
                }] ;
                this.paramscopy = [this.currentOrgId];
            },
            handleChangeOnSelect(value){
                if(value==true){
                    this.queryRoot();
                }
                this.changeOnSelect = value;
            },
            resetCurrentOrg(){
               /* this.paramsdatacopy.splice(1,1) ;
                this.paramscopy.splice(0,this.paramscopy.length);*/
                this.paramsdatacopy.splice(this.paramsdatacopy.length-1,1) ;
                this.paramscopy.splice(0,this.paramscopy.length);

            },
            Cascaderformat(labels, selectedData){
                if(this.multiple && this.paramscopy.length>0){
                    if(this.arrcopy.indexOf(labels[labels.length-1])==-1){
                        this.selectionarr.push(
                            {
                                value: this.paramscopy[this.paramscopy.length-1],
                                label: labels[labels.length-1]
                            }
                        )
                        this.selectiondata.push(this.paramscopy[this.paramscopy.length-1])
                        this.arrcopy.push(labels[labels.length-1])
                    }else{
                        if(this.selectiondata.indexOf(this.paramscopy[this.paramscopy.length-1])==-1){
                            this.selectiondata.push(this.paramscopy[this.paramscopy.length-1])
                        }
                    }
                    this.setselectionshow = false
                    this.paramscopy = []
                }
                if(!this.multiple && this.paramscopy.length>0){
                    this.$emit('setOrgInfo',this.paramscopy)
                    let data = {
                        orgName:labels[labels.length-1],
                        index:this.currentIndex
                    }
                    this.$emit('setOrgname',data)
                }
                return labels[labels.length-1];
            },
            loadData (item, callback) {
                item.loading = true;
                if(this.orgTree){
                    this.$http.get(Api.orgInfoTree,{
                        params:{
                            parentId:item.value,
                            codeAffordIf:this.codeAffordIf
                        }
                    }).then(res => {
                        item.loading = false
                        var resData = [];
                        res.data.data.map(i=>{
                            if(i.orgType == "1"){
                                i.value = i.orgId
                                delete i['orgId']
                                i.label = i.orgName
                                delete i['orgName']
                                i.loading=false
                                i.children = [];
                                resData.push(i);
                            }
                        })
                        if( resData.length > 0) {
                            item.children = resData;
                        }else {
                            delete item['children'];
                            delete item['loading'];
                        }
                        callback();
                    }).catch(e => {
                    })
                }else{
                    /*console.log(item.children);
                    if(item.children && item.children.length == 0 ){
                        console.log(11111111)
                        delete item['children'];
                    }
                    console.log(item.children);
                    if(item.children!=undefined){
                        console.log(2222)
                        this.$http.get(Api.orgUsers+item.value,{params:{}}).then((res) => {
                            if(res.data.data.length==0){
                                res.data=[{value:'',label:'暂无人员',disabled:true}]
                            }else{
                                res.data.data.map(j=>{
                                    j.value = j.userId
                                    delete j['userId']
                                    j.label = j.userName
                                    delete j['userName']
                                })
                            }
                            item.children = res.data.data;
                            item.loading=false;
                            callback();
                        }).catch(e => {
                            console.log(e)
                        })
                    }else{*/
                        this.$http.get(Api.orgInfoTree,{
                            params:{
                                parentId:item.value,
                                codeAffordIf:this.codeAffordIf
                            }
                        }).then(res => {
                            if(res.data.data.length!=0){
                                res.data.data.map(i=>{
                                    i.value = i.orgId
                                    delete i['orgId']
                                    i.label = i.orgName
                                    delete i['orgName']
                                    i.loading=false
                                    i.children = [];
                                })
                                item.children = res.data.data
                                item.loading=false
                                callback();
                            }else{
                                delete item['children']
                                item.loading=false
                                delete item['loading']

                                this.$http.get(Api.orgUsers+item.value,{params:{}}).then((res) => {
                                    if(res.data.data.length==0){
                                        res.data=[{value:'',label:'暂无人员',disabled:true}]
                                    }else{
                                        res.data.data.map(j=>{
                                            j.value = j.userId
                                            delete j['userId']
                                            j.label = j.userName
                                            delete j['userName']
                                        })
                                    }
                                    item.children = res.data.data;
                                    item.loading=false;
                                    callback();
                                }).catch(e => {
                                    console.log(e)
                                })
                            }
                        }).catch(e => {
                            console.log(e)
                        })
                    //}
                }
            },
            queryRoot(){
                let reqOrgId = '';
                if(this.rootOrgId!=undefined){
                    reqOrgId = this.rootOrgId;
                }
                else if(sessionStorage('userId').get('userId')){
                    reqOrgId = JSON.parse(sessionStorage('userId').getObject('userId').companyId);
                }else{
                    reqOrgId = -1;
                }
                this.$http.get(Api.parentOrgId+reqOrgId,{params:{}}).then(o => {
                    let parentId = o.data.data.orgId;
                    if(reqOrgId != -1){
                        this.paramsdatacopy = [{
                            value:o.data.data.orgId,
                            label:o.data.data.orgName,
                            children:[]
                        }]
                    }
                    this.$http.get(Api.orgInfoTree,{
                        params:{
                            //parentId: '-1',
                            parentId: parentId,
                            codeAffordIf:this.codeAffordIf
                        }
                    }).then(res => {
                        res.data.data.forEach(i=>{
                            i.value = i.orgId
                            delete i['orgId']
                            i.label = i.orgName
                            delete i['orgName']
                            i.loading=false
                            i.children = [];
                        })
                        if(reqOrgId != -1){
                            this.paramsdatacopy[0].children = res.data.data;
                        }else{
                            this.paramsdatacopy = res.data.data;
                        }
                    }).catch(e => {
                    })
                }).catch(e => {
                })
            },
        },

        computed: {
        },
        mounted() {

        }
    }
</script>

<style lang="less">
    /*修改分页样式*/

    .Cascader {
        display: block;
    }
</style>
